package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.InvitationCodes;
import com.ruoyi.system.service.IInvitationCodesService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 邀请管理Controller
 *
 * @author LIUSUJUN
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/codes")
public class InvitationCodesController extends BaseController
{
    @Autowired
    private IInvitationCodesService invitationCodesService;

    /**
     * 查询邀请管理列表
     */
    @RequiresPermissions("system:codes:list")
    @GetMapping("/list")
    public TableDataInfo list(InvitationCodes invitationCodes)
    {
        Long userid = SecurityUtils.getUserId();
        invitationCodes.setUserId(userid);
        startPage();
        List<InvitationCodes> list = invitationCodesService.selectInvitationCodesList(invitationCodes);
        return getDataTable(list);
    }

    /**
     * 导出邀请管理列表
     */
    @RequiresPermissions("system:codes:export")
    @Log(title = "邀请管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InvitationCodes invitationCodes)
    {
        List<InvitationCodes> list = invitationCodesService.selectInvitationCodesList(invitationCodes);
        ExcelUtil<InvitationCodes> util = new ExcelUtil<InvitationCodes>(InvitationCodes.class);
        util.exportExcel(response, list, "邀请管理数据");
    }

    /**
     * 获取邀请管理详细信息
     */
    @RequiresPermissions("system:codes:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(invitationCodesService.selectInvitationCodesById(id));
    }

    /**
     * 新增邀请管理
     */
    @RequiresPermissions("system:codes:add")
    @Log(title = "邀请管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InvitationCodes invitationCodes)
    {
        return toAjax(invitationCodesService.insertInvitationCodes(invitationCodes));
    }

    /**
     * 修改邀请管理
     */
    @RequiresPermissions("system:codes:edit")
    @Log(title = "邀请管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InvitationCodes invitationCodes)
    {
        return toAjax(invitationCodesService.updateInvitationCodes(invitationCodes));
    }

    /**
     * 删除邀请管理
     */
    @RequiresPermissions("system:codes:remove")
    @Log(title = "邀请管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(invitationCodesService.deleteInvitationCodesByIds(ids));
    }



    @GetMapping(value = "/createCodes")
    public String createCodes()
    {
        String substring = UUID.randomUUID().toString().substring(0, 8);
        // 保存邀请码与用户的关系
        InvitationCodes invitationCode = new InvitationCodes();
        invitationCode.setCode(substring);
        // 获取当前用户ID
        Long userid = SecurityUtils.getUserId();
        invitationCode.setUserId(userid);
        invitationCodesService.insertInvitationCodes(invitationCode);
        return "http://localhost/register?invitationCode=" + substring;
    }



}
